package com.gjm.demo;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.http.HttpResponse;

@RequestMapping("/response")
@Controller
public class ResponseCollection {
    //返回页面
    @RequestMapping("/m1")
    public String returnHTML1() {
        return "/hello.html";
    }

    //返回数据
    @ResponseBody
    @RequestMapping("/m2")
    public String returnData1() {
        return "返回数据";
    }

    @ResponseBody
    @RequestMapping("/m3")
    public String returnHTML2() {
        return "<h1>Hello</h1>";//返回一个页面
    }

    @ResponseBody
    @RequestMapping(value = "/m4", produces = "text/plain")
    public String returnData2() {
        return "<h1>Hello</h1>";//直接返回数据
    }

    @RequestMapping("/m5")
    @ResponseBody
    public UserInfo returnJson() {
        UserInfo userInfo = new UserInfo("张三", 12, 1);
        return userInfo;
    }

    @RequestMapping("/m6")
    @ResponseBody
    public UserInfo returnStatus(HttpServletResponse response) {
        response.setStatus(404);
        UserInfo userInfo = new UserInfo("张三", 12, 1);
        return userInfo;
    }

    @RequestMapping("/m7")
    @ResponseBody
    public String setHeader(HttpServletResponse response) {
        response.setHeader("myHeader", "myHeader");
        return "设置成功";
    }
}
